package org.wekit.engine.pvm.runtime;

import java.util.List;

import org.wekit.engine.pvm.PvmEvent;
import org.wekit.engine.pvm.process.Activity;
import org.wekit.engine.pvm.process.ProcessDefinition;
import org.wekit.engine.pvm.process.Scope;
/**
 * 
 * @author HuangWeili
 *
 */
public class AtomicOperationProcessStartInitial extends AbstractEventAtomicOperation {

	@Override
	protected Scope getScope(InterpretableExecution execution) {
		return (Scope)execution.getActivity();
	}

	@Override
	protected String getEventName() {
		return PvmEvent.EVENTNAME_START;
	}

	/**
	 * 研读这里的代码
	 */
	@Override
	protected void eventNotificationsCompleted(InterpretableExecution execution) {
     Activity activity=(Activity)execution.getActivity();
     ProcessDefinition processDefinition=execution.getProcessDefinition();
     if(activity==processDefinition.getInitial())
     {
    	 execution.performOperation(new AtomicOperationActivityExecute());
     }else{
    	 List<Activity> initialActivityStack=processDefinition.getInitialActivityStack();
    	 int index=initialActivityStack.indexOf(activity);
    	 activity=initialActivityStack.get(index+1);
    	 InterpretableExecution executionToUse=null;
    	 if(activity.isScope())
    	 {
    	   executionToUse=(InterpretableExecution)execution.getExecutions().get(0);	 
    	 }else{
    		 executionToUse=execution;
    	 }
    	 executionToUse.setActivity(activity);
    	 executionToUse.performOperation(new AtomicOperationProcessStartInitial()); 
     }
		
	}

	
	
}
